<?php

	/**
	 * @see Bike_Js_Ext_Obj
	 */
	require_once 'Bike/Js/Ext/Obj.php';
	
	
	abstract class Bike_Js_Ext_ComponentLoader extends Bike_Js_Ext_Obj
	{
		protected function initDefaultSettings()
		{
			parent::initDefaultSettings();
		
			$js = $this->js();
			$this->setSettings(array(
				'autoLoad' => true,
				'ajaxOptions' => array(
					'timeout' => 20000,
				),
				'listeners' => array(
	    		    'beforeload' 	=> $this->getJsListenersBeforeload(),
	    			'load' 			=> $this->getJsListenersLoad(),
					'exception' 	=> $this->getJsListenersException(),
	    		),
			));
		}
		
		/**
		 * Get JS listeners beforeload
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersBeforeload()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->loader, $js->options)->_body(array(
						$js->loader->getTarget()->addListener('show', $js->function($js->component)->_body(array(
							$js->if($js->_not($js->component->loader->loaded))->_body(array(
								$js->component->loader->loadMask->_val($js->Ext->LoadMask($js->component)),
							)),
						))),
						$js->_var($js->res)->_val($js->loader->getTarget()->fireEvent('beforeload', $js->loader->getTarget(), $js->loader, $js->options)),
						$js->if($js->res->_identity(false))->_body(array(
							$js->return(false),
						)),
					))
				);
			}
			return $this->getParamCache(__METHOD__);
		}
		
		/**
		 * Get JS listeners load
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersLoad()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->loader, $js->options)->_body(array(
						$js->loader->loaded->_val($js->true),
						$js->if($js->loader->loadMask)->_body(array(
							$js->loader->loadMask->hide(),
						)),
						$js->loader->getTarget()->fireEvent('load', $js->loader->getTarget(), $js->loader, $js->options),
					))
				);
			}
			return $this->getParamCache(__METHOD__);
		}
		
		/**
		 * Get JS listeners exception
		 *
		 * @return Bike_Js_Interface_Js object
		 */
		protected function getJsListenersException()
		{
			if(!$this->getParamCache(__METHOD__))
			{
				$js = $this->js();
				$this->setParamCache(__METHOD__,
					$js->function($js->loader, $js->response, $js->options)->_body(array(
						$js->_var($js->r)->_val($js->Ext->ResponseHandler->Failure->call($js->this, $js->loader, $js->response)),
						$js->if($js->loader->getTarget()->isPanel)->_body(array(
							$js->loader->getTarget()->close(),
						)),						
					))
				);
			}
			return $this->getParamCache(__METHOD__);
		}

		protected function _makeOptions(array $Args=null)
		{
			$options = array();
			$argsCount = sizeof($Args);
			if($argsCount == 1 && is_array($Args[0]))
			{
				$options = $Args[0];
			}
			else
			{				$options['url']	= @$Args[0];
			}
			return $options;
		}
	}
